﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Data;
using Microsoft.SharePoint.Utilities;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Xml;
using Disclaimer.Layouts.Disclaimer;

namespace Disclaimer.Disclaimer
{
    public partial class DisclaimerUserControl : UserControl
    {
        string strList = "Disclaimer";
        string AllFileContent;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                AddList();
                SPSite mySite = SPContext.Current.Site;
                SPWeb myweb = mySite.OpenWeb();
                SPList myList = myweb.Lists[strList];
                SPListItem myListItem = myList.GetItemById(1);
                AllFileContent = string.Empty;
                foreach (String attachmentname in myListItem.Attachments)
                {
                    String attachmentAbsoluteURL =
                    myListItem.Attachments.UrlPrefix // gets the containing directory URL
                    + attachmentname;
                     string strFileType = attachmentname.Substring(attachmentname.LastIndexOf("."));
                     if (strFileType.ToString().ToLower() == ".txt")
                     {
                         // To get the SPSile reference to the attachment just use this code
                         SPFile attachmentFile = myweb.GetFile(attachmentAbsoluteURL);
                         // To read the file content simply use this code
                         Stream stream = attachmentFile.OpenBinaryStream();
                         StreamReader reader = new StreamReader(stream);
                         String fileContent = reader.ReadToEnd();
                         AllFileContent += fileContent.Replace("\r\n", "<br/>");
                         divPageContent.InnerHtml = AllFileContent.ToString();
                     }
                     else
                     {
                         lblResults.Text = "Invalid file extension, Please select .txt file.";
                     }

                }
            }
            catch
            {

            }
        }

        // Create list
        private void AddList()
        {
            try
            {
                Boolean chkListExists;
                SPWeb myWeb = SharepointManager.GetCurrentWeb();

                chkListExists = SharepointManager.ListExists(myWeb, strList);

                if (!chkListExists)
                {
                    SharepointManager.CreateNewList(strList, strList, SPListTemplateType.GenericList);
                }
                using (SPSite sitecol = myWeb.Site)
                {
                    using (SPWeb myDifferentWeb = sitecol.OpenWeb())
                    {
                        myDifferentWeb.AllowUnsafeUpdates = true;
                        SPList list = myDifferentWeb.Lists[strList];
                        list.OnQuickLaunch = false;
                        list.Update();
                    }
                }
            }
            catch (Exception)
            {
                lblResults.Text = "You don't have sufficient priviledges to manage list";
            }
        }
    }
}
